local p = {}
local getArgs = require('Module:Arguments').getArgs
local inArray = require('Module:Util').inArray
local OrderedTable = require('Module:OrderedTable')

local TYPE_NAME_MAP = require('Module:Util/Config').TYPE_NAME_MAP 
local WEAPON_MAP = require('Module:Util/Config').WEAPON_MAP 
local PROFESSION_CLASS = require('Module:Util/Config').PROFESSION_CLASS
local split_swap = require('Module:Util/SwapImages').split_swap
local profession_map = require('Module:Util/Profession')

-----------------------------------------------------------------------------
-- 读取角色信息
-----------------------------------------------------------------------------

function p.query_data(profession_id)
	local card_info = profession_map[profession_id or ''] or ''
	card_info.id = profession_id
	return card_info
end

function p.get_by_series(series)
    local output = {}
	for _, card_info in pairs(profession_map) do
	    card_info.ID = _
		if (tonumber(card_info.ID) or 0) > 0 then
		    if (series or '') ~= '' and (card_info.series == series) then
				table.insert(output, card_info)
			end
		end
	end
	table.sort(output, p.sort_char_data_by_class)
	return output
end

function p.sort_char_data_by_class(a, b)
	return a.ID < b.ID
end

-----------------------------------------------------------------------------
-- 生成角色表格
-----------------------------------------------------------------------------

function p.left(frame)
	local args = getArgs(frame)
	local profession_id = tonumber(mw.text.split(args.pagename, '/')[2]) or tonumber(args.pagename)
	if not profession_id then
		return '【错误】提供的ID有错误'
	end
	
	local card_info = p.query_data(profession_id or '')

	if card_info == '' then
		return '【错误】没有找到ID相同的职业信息，请到职业列表进行更新修正。'
	end

	mw.ext.displaytitle.set(card_info.name)
	
	local half_html = '<div class="role-box element-0"><div class="role-left">'
	
	local html = mw.html.create():tag('div'):addClass('role-base-info')
	
	local title = html:tag('div'):addClass('role-base-title color-box color-frame')
	title:tag('div'):wikitext('['..(PROFESSION_CLASS[(card_info.class or '')] or '')..']'):done()
	title:tag('div'):wikitext((card_info.name or '')):done()
	title:done()
	
	local imgBox = html:tag('div'):addClass('role-base-img-box')
	--立绘
	
	local zoom_img_div = imgBox:tag('div'):addClass('role-base-img-group')
	local filter_div = imgBox:tag('div'):addClass('role-base-img-btn-group')
	
	local zoom_list = {'01', '02'}
	local zoom_name_map = {
		['01'] = '男主',
		['02'] = '女主',
	}
	local extra_zoom_list = {}
	for _, zoom_id in ipairs(mw.text.split(args['额外立绘'] or '', ',')) do
		zoom_id = tostring(zoom_id)
		if zoom_id ~= '' and not inArray(extra_zoom_list, zoom_id) then
			table.insert(extra_zoom_list, zoom_id)
		end
	end
	for _, zoom_id in ipairs(card_info.extra_img or {}) do
		zoom_id = tostring(zoom_id)
		if zoom_id ~= 'false' and  zoom_id ~= '' and not inArray(extra_zoom_list, zoom_id) then
			table.insert(extra_zoom_list, zoom_id)
		end
	end
	local extra_zoom_count=#extra_zoom_list
	for extra_zoom_index, extra_zoom_id in ipairs(extra_zoom_list) do
		if zoom_name_map_gender[extra_zoom_id] then
			extra_zoom_count=extra_zoom_count-1
			table.insert(zoom_list, extra_zoom_id)
		end
	end
	table.sort(zoom_list)
	
	for extra_zoom_index, extra_zoom_id in ipairs(extra_zoom_list) do
		if not zoom_name_map_gender[extra_zoom_id] then
			if extra_zoom_count == 1 then
				zoom_name_map[extra_zoom_id] = '额外'
			else
				zoom_name_map[extra_zoom_id] = '额外'..extra_zoom_index
			end
			table.insert(zoom_list, extra_zoom_id)
		end
	end
	
	local dataid = 0
	for _, zoom_id in ipairs(zoom_list) do
		local zoom_div = zoom_img_div:tag('div'):addClass('role-base-img'):attr('data-id', dataid)
		local filter_btn = filter_div:tag('div'):addClass('role-base-img-btn border-color'):attr('data-id', dataid):wikitext(zoom_name_map[zoom_id])
 
		if zoom_id == '01' then
			filter_btn:addClass('active')
		end
		local zoom_id_text = tostring(zoom_id)
		if #zoom_id_text == 1 then
			zoom_id_text = '0'..zoom_id_text
		end
 
		zoom_div:wikitext('[[File:'..(card_info.id or '')..'_'..zoom_id_text..'.png|600px]]')
		dataid = dataid + 1
	end
	
	return half_html..tostring(html)
end


function p.right(frame)
	local args = getArgs(frame)
	local profession_id = tonumber(mw.text.split(args.pagename, '/')[2]) or tonumber(args.pagename)
	if not profession_id then
		return '【错误】提供的ID有错误'
	end
	
	local card_info = p.query_data(profession_id or '')

	if card_info == '' then
		return '【错误】没有找到ID相同的职业信息，请到职业列表进行更新修正。'
	end
	
	local half_html = '</div><div class="role-right"><div class="role-info color-box color-frame">'
	
	local html = mw.html.create()
	html:tag('div'):addClass('role-right-title'):wikitext('基本信息'):done()
	html:tag('div'):addClass('role-right-box-2 color-frame-2'):tag('div'):addClass('role-right-box-title'):wikitext('类型'):done():tag('div'):addClass('role-right-box-content'):wikitext('[[File:CharType_'..(card_info.type or '')..'.png|25px]] '..(TYPE_NAME_MAP[(card_info.type or '')] or '')):done():done()
	
	local weapon = '[[File:CharWeapon_'..(card_info.weapon1 or '')..'.png|25px]] '..(WEAPON_MAP[(card_info.weapon1 or '')] or '')
	if (card_info.weapon2 or '') ~= '' then
		weapon = weapon .. '[[File:CharWeapon_'..(card_info.weapon2 or '')..'.png|25px]] '..(WEAPON_MAP[(card_info.weapon2 or '')] or '')
	end	
	html:tag('div'):addClass('role-right-box-2 color-frame-2'):tag('div'):addClass('role-right-box-title'):wikitext('得意武器'):done():tag('div'):addClass('role-right-box-content'):wikitext(weapon):done():done()
	
	local series_table = p.get_by_series(card_info.series or '')
	local series_text = ''
	for _, info in ipairs(series_table) do
		series_text = series_text..'[[File:icon_'..info.ID..'.png|25px]]'..'[[Leader/'..info.ID..']]<br>'
	end
	
	if (series_text or '') ~= '' then
	html:tag('div'):addClass('role-right-box color-frame'):tag('div'):addClass('role-right-box-title'):wikitext('职业线'):done():tag('div'):addClass('role-right-box-content'):wikitext(series_text):done():done()
	end
	
	html:tag('div'):addClass('role-right-box color-frame'):tag('div'):addClass('role-right-box-title'):wikitext('加入时间'):done():tag('div'):addClass('role-right-box-content'):wikitext((os.date("%Y年%m月%d日",card_info.addDate) or '')):done():done()
	
	if (args['解放武器'] or card_info.unlockWeapon or '') ~= '' then
		html:tag('div'):addClass('role-right-box color-frame-2'):tag('div'):addClass('role-right-box-title'):wikitext('解放武器'):done():tag('div'):addClass('role-right-box-content'):wikitext('[[Weapon/'..(args['解放武器'] or card_info.unlockWeapon or '')..']]'):done():done()
	end

	if (args['职业介绍'] or '') ~= '' then
	html:tag('div'):addClass('role-right-box color-frame'):tag('div'):addClass('role-right-box-title'):wikitext('职业介绍'):done():tag('div'):addClass('role-right-box-content'):wikitext(args['职业介绍'] or ''):done():done()
	end
		
	html:tag('div'):addClass('role-right-box color-frame'):tag('div'):addClass('role-right-box-title'):wikitext('取得条件'):done():tag('div'):addClass('role-right-box-content'):wikitext(args['取得条件'] or ''):done():done()
	
	local level_table = mw.html.create():tag('table')
	for _, index in ipairs({'1', '5', '10', '15', '20', '25', '30'}) do
		if (args['等级加成'..index] or '') ~= '' then
			local level_tr = level_table:tag('tr')
			level_tr:tag('th'):cssText('width:42px;'):wikitext(index..'级')
			level_tr:tag('td'):wikitext(args['等级加成'..index])
		end
	end
	
	local level_total = mw.html.create():tag('table')
	if (args['等级加成总计'] or '') ~= '' then
		local level_tr = level_total:tag('tr')
		level_tr:tag('th'):cssText('width:42px;'):wikitext('总计')
		level_tr:tag('td'):wikitext(args['等级加成总计'])
	end
	
	html:tag('div'):addClass('role-right-box color-frame'):tag('div'):addClass('role-right-box-title'):wikitext('等级加成'):done():tag('div'):addClass('role-right-box-content'):addClass('text-left'):tag('div'):cssText('margin-left:5px;'):node(level_table):done():tag('div'):cssText('margin-left:5px;'):node(level_total)
	
	if (args['全职业加成'] or '') ~= '' then
		html:tag('div'):addClass('role-right-box color-frame'):tag('div'):addClass('role-right-box-title'):wikitext('全职业加成(20级)'):done():tag('div'):addClass('role-right-box-content'):wikitext(args['全职业加成'])
	end

	return half_html..tostring(html)..'</div></div></div>'
end

return p
